package com.jingan.nursinghome.regulatory.separate;

import android.util.JsonReader;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.data.Scount;
import com.jingan.nursinghome.regulatory.http.Http;
import com.jingan.nursinghome.regulatory.model.Linkup;
import com.jingan.nursinghome.regulatory.separate.bean.Mession;
import com.jingan.nursinghome.regulatory.view.MyActionBar;
import com.jingan.nursinghome.regulatory.view.SpannerChildView;
import com.jingan.nursinghome.regulatory.view.SpannerChildView2;
import com.jingan.nursinghome.regulatory.view.SpannerView;
import com.jingan.nursinghome.regulatory.view.liView;
import com.jingan.nursinghome.regulatory.view.ryView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.dialog.DateDialog;
import cn.android.support.v7.lib.sin.crown.dialog.ProgressDailog;
import cn.android.support.v7.lib.sin.crown.utils.PopuWindowUtils;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;
import cn.android.support.v7.lib.sin.crown.utils.TimeUtils;

/**
 * Created by 彭治铭 on 2017/9/29.
 */

public class CreateSeparateDailog extends BaseDialog {

    private CreateSeparateDailog(BaseActivity activity) {
        init(activity, R.layout.dialog_create_micrometertablle, false);
        window.setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.crown_window_right);
        BaseApplication.getInstance().setStatusBarDrak(window, true);
        initUI();
        //adapterUI();
    }

    @Override
    protected boolean isDismiss() {
        return false;
    }

    MyActionBar myActionBar;
    TextView appraise;
    ryView txt_ry;

    liView timu, jly, tm, ry, qt; //timu 题目类型

    EditText edit_qt;//其他
    SpannerView txt_timu, txt_jly, txt_tm, txt_ry1, txt_ry2;
    PopupWindow timupopu, jlypopu, ry1popu, ry2popu;//敬老院弹窗


    List<String> stnames = null;

    public void initUI() {

        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "新建千分表巡检");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                dismiss();
            }
        });
        timu = (liView) findViewById(R.id.timu);
        timu.setLeftAndInfo("图标/题目类型.png", "题目类型");
        jly = (liView) findViewById(R.id.jly);
        jly.setLeftAndInfo("图标/敬老院.png", "养老机构");
        tm = (liView) findViewById(R.id.tm);
        tm.setLeftAndInfo("图标/时间.png", "时间");
        ry = (liView) findViewById(R.id.ry);
        ry.setLeftAndInfo("图标/主要沟通人员.png", "主要沟通人员");
        qt = (liView) findViewById(R.id.qt);
        qt.setLeftAndInfo("图标/其他.png", "其他");
        txt_ry = (ryView) findViewById(R.id.txt_ry);
        appraise = (TextView) findViewById(R.id.appraise);
        appraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //开始评分
            }
        });
        edit_qt = (EditText) findViewById(R.id.edit_qt);
        txt_timu = (SpannerView) findViewById(R.id.txt_timu);
        txt_jly = (SpannerView) findViewById(R.id.txt_jly);
        txt_jly.setInfo("请选择千分表巡检的养老机构");
        txt_tm = (SpannerView) findViewById(R.id.txt_tm);
        if (MM < 10) {
            M = "0" + MM;
        } else {
            M = MM + "";
        }
        if (dd < 10) {
            D = "0" + dd;
        } else {
            D = dd + "";
        }
        txt_tm.setInfo(yyyy + "-" + M + "-" + D);
        txt_ry1 = (SpannerView) findViewById(R.id.txt_ry1);
        txt_ry1.setInfo("评估人员");
        txt_ry2 = (SpannerView) findViewById(R.id.txt_ry2);
        txt_ry2.setInfo("养老机构负责人");


        //题目类型
        txt_timu.setInfo("请选择题目");
        txt_timu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Log.e("test","题目:\t"+timupopu+"\t"+stnames);
//                if(stnames!=null){
//                    Log.e("test","题目个数:\t"+stnames.size());
//                }
                if (timupopu == null || stnames == null || stnames.size() <= 0) {
                    SpannerChildView spannerChildView = new SpannerChildView(jly.getContext());
                    stnames = MicrometerTable.getInstance().getNames();
                    spannerChildView.setInfos(txt_timu, stnames, stnames.size(), new BaseListenner<String>() {
                        @Override
                        public void onResult(String result) {
                            txt_timu.setInfo(result);
                            rid = result;
                            if (timupopu != null) {
                                timupopu.dismiss();
                            }
                        }
                    });
                    timupopu = PopuWindowUtils.getInstance().showPopuWindow(spannerChildView, cn.android.support.v7.lib.sin.crown.R.style.crown_window_alpha);
                    timupopu.showAsDropDown(txt_timu);
                } else {
                    timupopu.showAsDropDown(txt_timu);
                }
            }
        });


        //敬老院
        txt_jly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (jlypopu == null) {
                    SpannerChildView spannerChildView = new SpannerChildView(jly.getContext());
                    List<String> infos = new ArrayList<String>();
                    for (int i = 0; i < MicrometerTable.getInstance().getJlys().size(); i++) {
                        infos.add(MicrometerTable.getInstance().getJlys().get(i).getName());
                    }
                    spannerChildView.setInfos(txt_jly, infos, (int) (txt_jly.getResources().getDisplayMetrics().heightPixels - txt_jly.getY() - txt_jly.getHeight()) / txt_jly.getHeight(), new BaseListenner<String>() {
                        @Override
                        public void onResult(String result) {
                            if (result == null) {
                                return;
                            }
                            jlyID = MicrometerTable.getInstance().getJlyID(result);
                            if (jlyName == null || jlyName.trim().equals("") || !jlyName.equals(result)) {
                                ProgressDailog.buider(activity);
                                ry1popu = null;
                                ry2popu = null;
                                txt_ry.setInfo("");
                                ry1 = "";
                                ry2 = "";
                                Scount.counts = new int[]{0, 0, 0};//主要沟通人员的勾选数量
                                linkups.clear();
                                Http.getInstance().getLinkUP(jlyID, new BaseListenner<List<Linkup>>() {
                                    @Override
                                    public void onResult(List<Linkup> result) {
                                        if (result != null && result.size() > 0) {
                                            linkups.clear();
                                            linkups.addAll(result);
                                        }
                                        ProgressDailog.tear(activity);
                                    }
                                });
                            }
                            jlyName = result;
                            txt_jly.setInfo(result);
                        }
                    });
                    jlypopu = PopuWindowUtils.getInstance().showPopuWindow(spannerChildView, cn.android.support.v7.lib.sin.crown.R.style.crown_window_alpha);
                    jlypopu.showAsDropDown(txt_jly);
                    spannerChildView.setPopupWindow(jlypopu);
                } else {
                    jlypopu.showAsDropDown(txt_jly);
                }
            }
        });

        //时间选择器
        txt_tm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DateDialog.getInstance(baseActivity, yyyy + "", MM + "", dd + "", new BaseListenner<List<String>>() {
                    @Override
                    public void onResult(List<String> result) {
                        yyyy = Integer.valueOf(result.get(0));
                        MM = Integer.valueOf(result.get(1));
                        dd = Integer.valueOf(result.get(2));
                        if (MM < 10) {
                            M = "0" + MM;
                        } else {
                            M = MM + "";
                        }
                        if (dd < 10) {
                            D = "0" + dd;
                        } else {
                            D = dd + "";
                        }
                        txt_tm.setInfo(yyyy + "-" + M + "-" + D);
                    }
                });
            }
        });
        txt_ry1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //评估人员
                if (ry1popu == null) {
                    SpannerChildView2 spannerChildView = new SpannerChildView2(txt_ry1.getContext());
                    spannerChildView.setIndex(0);

                    List<String> infos = new ArrayList<String>();
                    for (int i = 0; i < MicrometerTable.getInstance().linkups1.size(); i++) {
//                        if (MicrometerTable.getInstance().linkups1.get(i).getLtype().equals("1")) {
                        infos.add(MicrometerTable.getInstance().linkups1.get(i).getName());
//                        }
                    }
                    if (infos == null || infos.size() <= 0) {
                        baseActivity.SnackbarMake("暂无评估人员", true, findViewById(R.id.dialog_create_micrometertable_constraintLayout));
                        return;
                    }
                    spannerChildView.setInfos(txt_ry1, infos, 4, new BaseListenner<String>() {
                        @Override
                        public void onResult(String result) {
                            ry1 = result;
                            if (ry2.trim().equals("")) {
                                txt_ry.setInfo(ry1);
                            } else {
                                txt_ry.setInfo(ry1 + "、" + ry2);
                            }
                            if (ry1popu != null) {
                                ry1popu.dismiss();
                            }
                        }
                    });
                    ry1popu = PopuWindowUtils.getInstance().showPopuWindow(spannerChildView, cn.android.support.v7.lib.sin.crown.R.style.crown_window_alpha);
                    ry1popu.showAsDropDown(txt_ry1);

                } else {
                    ry1popu.showAsDropDown(txt_ry1);
                }
            }
        });
        txt_ry2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //养老机构负责人
                if (ry2popu == null) {
                    if (jlyID == null || jlyID.trim().equals("")) {
                        baseActivity.SnackbarMake("请先选择敬老院", true, findViewById(R.id.dialog_create_micrometertable_constraintLayout));
                        return;
                    }
                    SpannerChildView2 spannerChildView = new SpannerChildView2(txt_ry2.getContext());
                    spannerChildView.setIndex(0);
                    List<String> infos = new ArrayList<String>();
                    for (int i = 0; i < linkups.size(); i++) {
//                        if (linkups.get(i).getLtype().equals("2")) {
                        infos.add(linkups.get(i).getName());
//                        }
                    }
                    if (infos == null || infos.size() <= 0) {
                        baseActivity.SnackbarMake("该敬老院暂无养老机构负责人", true, findViewById(R.id.dialog_create_micrometertable_constraintLayout));
                        return;
                    }
                    spannerChildView.setInfos(txt_ry2, infos, 4, new BaseListenner<String>() {
                        @Override
                        public void onResult(String result) {
                            ry2 = result;
                            if (ry1.trim().equals("")) {
                                txt_ry.setInfo(ry2);
                            } else {
                                txt_ry.setInfo(ry1 + "、" + ry2);
                            }
                            if (ry2popu != null) {
                                ry2popu.dismiss();
                            }

                        }
                    });
                    ry2popu = PopuWindowUtils.getInstance().showPopuWindow(spannerChildView, cn.android.support.v7.lib.sin.crown.R.style.crown_window_alpha);
                    ry2popu.showAsDropDown(txt_ry2);
                } else {
                    ry2popu.showAsDropDown(txt_ry2);
                }
            }
        });
        appraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                time = txt_tm.getInfo();//日期时间
                names = txt_ry.getInfo();//主要沟通人员
                other = edit_qt.getText().toString().trim();//其他
//                String timu = txt_timu.getInfo();//题目类型
                if (rid == null) {
                    baseActivity.SnackbarMake("请选择题目类型", true, findViewById(R.id.dialog_create_micrometertable_constraintLayout));
                    return;
                }
                if (jlyName == null) {
                    baseActivity.SnackbarMake("请选择敬老院", true, findViewById(R.id.dialog_create_micrometertable_constraintLayout));
                }
//                else if (names == null || names.trim().equals("")) {
//                    baseActivity.SnackbarMake("沟通人员不可为空", true, findViewById(R.id.dialog_create_micrometertable_constraintLayout));
//                }
                else {

                    //fixme 跳转=====================================================================
                    // CreateSeparateDailog.jlyName 巡检机构
                    //CreateSeparateDailog.jlyID  巡检机构ID
                    // CreateSeparateDailog.time 巡检时间
                    // CreateSeparateDailog.names 主要沟通人员
                    // CreateMicrometerTableDailog.rid 套题名称
                    // CreateSeparateDailog.other 其他

                    String id2 = MicrometerTable.getInstance().getUser().getId();

                    //主要沟通人员ID
                    String[] lids = CreateSeparateDailog.names.split("、");
                    String lids2 = "";
                    for (int i = 0; i < lids.length; i++) {
                        String id = MicrometerTable.getInstance().getLinkupID(lids[i]);
                        if (i == 0) {
                            lids2 = id;
                        } else {
                            lids2 = lids2 + "," + id;
                        }
                    }
                    Uri.INSTANCE.fbtj(activity, rid, jlyID, time, lids2, other, id2, new BaseListenner<Mession>() {
                        @Override
                        public void onResult(Mession result) {
                            if (result.getCode().equals("0")) {
                                //发布成功
                                //Log.e("ui","id:\t"+result.getList());
                                try {
                                    JSONObject json = new JSONObject(result.getList());
                                    new CreateSeparateDailog2(baseActivity, json.getString("xj_id"), json.getString("pid"));
                                    dismiss();
                                } catch (Exception e) {
                                    Log.e("ui", "发布成功JSON异常:\t" + e.getMessage());
                                }
                            } else {
                                baseActivity.SnackbarMake(result.getErrmsg(), true, findViewById(R.id.dialog_create_micrometertable_constraintLayout));
                            }
                        }
                    });


                }
            }
        });
    }

    public static String other;//其他
    public static String jlyName;//敬老院名称\

    public static String jlyID;//敬老院id
    List<Linkup> linkups = new ArrayList<>();//沟通人员

    public static String rid;//题目模型
    public static String time;//时间
    public static String names;//主要沟通人员
    //public static int selectCount = 0;//评估人员 与 主管部分人员。最多可选五人

    String ry1 = "", ry2 = "";//選中人名單

    int yyyy = TimeUtils.getInstance().getYear(null);
    int MM = TimeUtils.getInstance().getMonth(null);
    int dd = TimeUtils.getInstance().getDay(null);
    String M, D;

    public void adapterUI() {
        //UtilProportion.getInstance(baseActivity).adapterScreen(baseActivity, findViewById(R.id.dialog_create_micrometertable_constraintLayout));
        ProportionUtils.getInstance().adapterScreen(baseActivity, findViewById(R.id.dialog_create_micrometertable_constraintLayout));
        ProportionUtils.getInstance().adapterTextView(appraise);
        ProportionUtils.getInstance().adapterView(findViewById(R.id.line1));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.line2));
        ProportionUtils.getInstance().adapterView(findViewById(R.id.line3));
        ProportionUtils.getInstance().adapterTextView(edit_qt);
        ProportionUtils.getInstance().adapterView(txt_timu);
        ProportionUtils.getInstance().adapterView(txt_jly);
        ProportionUtils.getInstance().adapterView(txt_tm);
        ProportionUtils.getInstance().adapterView(txt_ry1);
        ProportionUtils.getInstance().adapterView(txt_ry2);
    }

    public static CreateSeparateDailog createMicrometerTableDailog;

    public static CreateSeparateDailog getInstance(BaseActivity activity) {
        if (isfinish(activity, createMicrometerTableDailog)) {
            createMicrometerTableDailog = new CreateSeparateDailog(activity);
        } else {
            createMicrometerTableDailog.show();
        }
        return createMicrometerTableDailog;
    }

    @Override
    protected void listener() {
        MicrometerTable.getInstance().topicsHostoryQF.clear();//清楚千分表历史整改
    }

    @Override
    protected void recycleView() {
        if (edit_qt != null) {
            edit_qt.clearFocus();
        }
    }

    @Override
    protected boolean isRecycel() {
        return false;
    }
}
